package com.scsj.supermarket.view.fragment.twokindsbasemodel;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jakewharton.rxbinding.support.v7.widget.RecyclerViewChildAttachEvent;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.ShopListBaseAdapter;
import com.scsj.supermarket.adapter.ShopTradeCircleAdapter;
import com.scsj.supermarket.base.QueryGoodsInTradeBase;
import com.scsj.supermarket.bean.QueryGoodsInTradeBean;
import com.scsj.supermarket.bean.RequestGoodsInTradeBean;
import com.scsj.supermarket.bean.RequestShopInTradeBean;
import com.scsj.supermarket.bean.ShopListBaseBean;
import com.scsj.supermarket.presenter.QueryGoodsInTradePresenter;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;

/**
 * 店铺基础列表
 */
public class ShopListBaseFragment extends BaseFragment implements QueryGoodsInTradeBase.IView{
    public  String searchText;
    public  boolean startSearch=false;
    private RecyclerView rv;
    private ShopTradeCircleAdapter adapter;
    private List<ShopListBaseBean> data;
    private QueryGoodsInTradePresenter queryGoodsInTradePresenter;

    private String tradeId;
    private double lat;
    private double lon;

    //用来存放所有商圈下商品的集合
    private List<QueryGoodsInTradeBean.DataBean.ListBean> goodsInTrades= new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_shop_list_base, null);

        // ScreenAdapterTools.getInstance().loadView(inflate);

        initView(inflate, null);
        initListener();
        initData();

        return inflate;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        queryGoodsInTradePresenter= new QueryGoodsInTradePresenter(this);
        rv = view.findViewById(R.id.shop_list_rv);
        rv.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        rv.setNestedScrollingEnabled(false);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        startGetData();
    }

    public void startGetData() {
        //Log.e("shop::",searchText);
        tradeId = getActivity().getIntent().getStringExtra("tradeId");
        lat = getActivity().getIntent().getDoubleExtra("lat", 0);
        lon = getActivity().getIntent().getDoubleExtra("lon", 0);

        //默认查询商圈下面所有的店铺

        RequestGoodsInTradeBean requestShopInTradeBean = new RequestGoodsInTradeBean();
        RequestGoodsInTradeBean.PageInfoBean pageInfoBean= new RequestGoodsInTradeBean.PageInfoBean();
        requestShopInTradeBean.setId(tradeId);
        requestShopInTradeBean.setName(searchText);

        //设置每页加载的个数
        // pageInfoBean.setPageNum();
        //设置页数
        // pageInfoBean.setPageSize();
        Gson gson = new Gson();
        String obj = gson.toJson(requestShopInTradeBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        queryGoodsInTradePresenter.deliverData(body);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void showSuccessMsg(String s, QueryGoodsInTradeBean queryGoodsInTradeBean) {
        goodsInTrades= queryGoodsInTradeBean.getData().getList();
        adapter = new ShopTradeCircleAdapter(goodsInTrades);
        rv.setAdapter(adapter);
    }

    @Override
    public void showErrorMsg(String s) {

    }

}
